package com.firebird.app.fekpsl.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeFriendlyUtil {
    //系统拽出的判断时间方法，正确
    public static String friendlyTime(String sdate) {

//        Date time = toDate(sdate);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = null;
        try {
            time = sf.parse(sdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //LogUtil.w("aaaaaaaa+++++++++++++++++++++++++",time.getTime()+"");
        //获取time距离当前的秒数
        int ct = (int)((System.currentTimeMillis() - time.getTime())/1000);

        if(ct == 0) {
            return "刚刚";
        }

        if(ct > 0 && ct < 60) {
            return ct + "秒前";
        }

        if(ct >= 60 && ct < 3600) {
            return Math.max(ct / 60,1) + "分钟前";
        }
        if(ct >= 3600 && ct < 86400)
            return ct / 3600 + "小时前";
        if(ct >= 86400 && ct < 2592000){ //86400 * 30
            int day = ct / 86400 ;
            return day + "天前";
        }
        if(ct >= 2592000 && ct < 31104000) { //86400 * 30
            return ct / 2592000 + "月前";
        }
        return ct / 31104000 + "年前";
    }
}
